package com.fqgj.xjd.user.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.base.BaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.dao.UserKycVoterAuthResultDao;
import com.fqgj.xjd.user.entity.UserKycVoterAuthResultEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * User: linkechao
 * Date: 2018-12-19
 * Time: 15:13:06.204
 */

@Repository
public class UserKycVoterAuthResultDaoImpl extends AbstractBaseMapper<UserKycVoterAuthResultEntity> implements UserKycVoterAuthResultDao {

    @Override
    public void addUserKycVoterAuthResult(UserKycVoterAuthResultEntity entity) {
        if (Objects.isNull(entity) || StringUtils.isBlank(entity.getUserCode())) {
            return;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", entity.getUserCode());
        List<UserKycVoterAuthResultEntity> userKycVoterAuthResultEntities = this.selectByParams(param);
        if (CollectionUtils.isEmpty(userKycVoterAuthResultEntities)) {
            this.insert(entity);
            return;
        }
        UserKycVoterAuthResultEntity userKycVoterAuthResultEntity = userKycVoterAuthResultEntities.get(0);
        BeanUtils.copyProperties(entity, userKycVoterAuthResultEntity);
        this.updateByPrimaryKey(userKycVoterAuthResultEntity);
    }

    @Override
    public UserKycVoterAuthResultEntity selectByUserCode(String userCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectByUserCode"), param);
    }
}